package com.baizan.daka.ui.fragment

import android.content.DialogInterface
import android.graphics.Color
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.baizan.base.BaseFragment
import com.baizan.daka.Constants
import com.baizan.daka.copyContent
import com.baizan.daka.event.TastMrgEvent
import com.baizan.daka.event.UserInfoEvent
import com.baizan.daka.glide.ImageLoadHelp
import com.baizan.daka.mvp.model.bean.BankCardInfoBean
import com.baizan.daka.ui.contract.MeContract
import com.baizan.daka.mvp.model.bean.MeModelBean
import com.baizan.daka.mvp.model.bean.UserInfoBean
import com.baizan.daka.mvp.model.bean.UserPackageDes
import com.baizan.daka.ui.presenter.MePresenter
import com.baizan.daka.showToast
import com.baizan.daka.ui.activity.*
import com.baizan.daka.ui.adapter.MeModelAdapter
import com.baizan.daka.util.HyperTextUtil
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.frg_me.*
import kotlinx.android.synthetic.main.frg_me.user_icon_iv
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MeFragment : BaseFragment<MePresenter>(), MeContract.View {


    companion object {
        val modeData = listOf(
//            MeModelBean(R.mipmap.icon_me_model_1,"我的店铺"),

            MeModelBean(R.mipmap.icon_me_model_6, "我的邀请码"),
            MeModelBean(R.mipmap.icon_me_model_7, "我的团队"),
            MeModelBean(R.mipmap.icon_me_model_2, "发布任务"),
            MeModelBean(R.mipmap.icon_me_model_3, "任务管理"),
            MeModelBean(R.mipmap.icon_me_model_0, "我的套餐"),
            MeModelBean(R.mipmap.icon_me_model_4, "提现记录"),
            MeModelBean(R.mipmap.icon_me_model_5, "资金明细")
        )
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun upOrDownEvent(event: UserInfoEvent) {
        Constants.userInfoBean?.let {
            loadSuccess(it)
        }
    }

    val adapter: MeModelAdapter by lazy { MeModelAdapter() }
    override fun initPresenter(): MePresenter = MePresenter(this)

    override fun layoutId(): Int = R.layout.frg_me

    override fun initData() {

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden)
            presenter?.getUserInfo()
    }

    override fun initView() {
        var mLinearLayoutManager = LinearLayoutManager(context)
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL)
        recycler_view?.setLayoutManager(mLinearLayoutManager)
        recycler_view?.setAdapter(adapter)
        adapter.setNewData(modeData)
        adapter.setOnItemClickListener { _, _, position ->
            when (position) {
                2 -> {
                    presenter?.loaduserPackageDes()

                }
                3 -> context?.let {
                    TaskMgrActivity.start(it)
                }
                5 -> {
                    context?.let {
                        ShowCashActivity.start(it)
                    }
                }
                6 -> context?.let {
                    CapitalDesListActivity.start(it)
                }
                0 -> context?.let {
                    ExtensionActivity.start(it)
                }
                1 -> context?.let {
                    MyTeamActivity.start(it)
                }
                4 -> context?.let {
                    MyPackageActivity.start(it)
                }
            }
        }
        user_modity_iv.setOnClickListener {
            context?.let {
                val c = it
                Constants.userInfoBean?.let {
                    UserInfoActivity.start(c, it)
                } ?: let {
                    showToast("用户数据异常")
                }

            }
        }
        refresh_layout.setOnRefreshListener {
            presenter?.getUserInfo()
        }
        task1_tv.setOnClickListener {
            context?.let {
                TaskHomeActivity.start(it, 1)
            }
        }
        task2_tv.setOnClickListener {
            context?.let {
                TaskHomeActivity.start(it, 2)
            }
        }
        task3_tv.setOnClickListener {
            context?.let {
                TaskHomeActivity.start(it, 3)
            }
        }
        bg_me_view.setOnClickListener {
            context?.let {
                SettingActivity.start(it)
            }
        }
        user_service_iv.setOnClickListener {
            mContext?.let {
                ServiceActivity.start(it)
            }
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun start() {
        EventBus.getDefault().register(this)
        presenter?.getUserInfo()
    }

    override fun loadSuccess(userInfoBean: UserInfoBean) {
        refresh_layout.refreshComplete()
        Constants.userInfoBean = userInfoBean

        user_account_tv.text = "昵称:${
        if (userInfoBean.userName.isNullOrEmpty()) userInfoBean.account else userInfoBean.userName}"
        when (userInfoBean.vipLevel) {
            0 -> user_vip_tv.setTextColor(Color.WHITE)
            1 -> user_vip_tv.setTextColor(resources.getColor(R.color.task_1_color))
            2 -> user_vip_tv.setTextColor(resources.getColor(R.color.task_2_color))
            3 -> user_vip_tv.setTextColor(resources.getColor(R.color.task_3_color))
            else -> user_vip_tv.setTextColor(resources.getColor(R.color.task_4_color))
        }
        lastTime_tv.text = "最后登录时间:${userInfoBean.lastLoginTime}"
        user_vip_tv.text = "会员等级：${userInfoBean.vipLevelName}"
        code.text = "邀请码：${userInfoBean.invitationCode}"
        ImageLoadHelp.loadImage(user_icon_iv, userInfoBean.headerUrl)
        cash_out_tv.tag = userInfoBean.userCapital.balanceMoney
        user_balance_tv.text = HyperTextUtil.build("账户余额：")
            .appendColor("${userInfoBean.userCapital.balanceMoney}元", Color.parseColor("#1B78FD"))
            .Builder()
        task_momey_value_tv.text = userInfoBean.userProfit.profitMoney
        task_momey_value2_tv.text = userInfoBean.userProfit.commissionMoney
        vip_money_value_tv.text = userInfoBean.userProfit.royaltyMoney
        today_momey_value_tv.text = userInfoBean.userProfit.vipMoney


        used_task_1_tv.text = "已做：${userInfoBean.userTask.usedJuniorTaskNumber}"
        cando_task_1_tv.text = "可做：${userInfoBean.userTask.doJuniorTaskNumber}"
        used_task_2_tv.text = "已做：${userInfoBean.userTask.usedMiddleTaskNumber}"
        cando_task_2_tv.text = "可做：${userInfoBean.userTask.doMiddleTaskNumber}"
        cando_task_3_tv.text = "可做：${userInfoBean.userTask.doSeniorTaskNumber}"
        used_task_3_tv.text = "已做：${userInfoBean.userTask.usedSeniorTaskNumber}"

        cando_task_4_tv.text = "可做：${userInfoBean.userTask.doSuperTaskNumber}"
        used_task_4_tv.text = "已做：${userInfoBean.userTask.usedSuperTaskNumber}"
        cash_out_tv.setOnClickListener {
            presenter?.getBankInfo()
        }
        copy.setOnClickListener {
            mContext?.let {
                copyContent(it, userInfoBean.invitationCode)
                showToast("复制成功")
            }
        }
    }

    override fun getBankInfoSuccess(bankCardInfoBean: BankCardInfoBean?) {
        context?.let {
            ShowCashDesActivity.start(it, cash_out_tv.tag.toString().toDouble(), bankCardInfoBean!!)
        }
    }

    override fun getBankInfoFail() {
        context?.let {
            var dialog = AlertDialog.Builder(it)
            dialog.setTitle("提示")
            dialog.setMessage("当前没有绑定银行卡，需要绑定银行卡，\n是否去绑卡?")
            dialog.setPositiveButton("马上绑卡", object : DialogInterface.OnClickListener {

                override fun onClick(dialog: DialogInterface?, which: Int) {
                    AddBankCardActivity.start(it)
                }

            })
            dialog.setNegativeButton("暂不绑卡", object : DialogInterface.OnClickListener {

                override fun onClick(dialog: DialogInterface, which: Int) {
                    dialog.dismiss()
                }

            })
            dialog.create().show()

        }
    }

    override fun loaduserPackageDesSuccess(userPackageDes: UserPackageDes) {
        context?.let {
            PublishTaskActivity.start(it, userPackageDes)
        }
    }

    override fun loadUserPackageDesFail(fail: String) {
        mContext?.let {
            var dialog = AlertDialog.Builder(it)
            dialog.setTitle("提示")
            dialog.setMessage(fail)
            dialog.setPositiveButton("确定") { dialog, which ->
                FlowPackageActivity.start(it)
            }
            dialog.setNegativeButton("取消") { dialog, which ->
                dialog.dismiss()
            }
            dialog.create().show()
        }
    }
}